<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"tallgy.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>
<meta name="description" content="只是一个知识的搬运工">
<meta property="og:type" content="website">
<meta property="og:title" content="tallgy&#39;s blog">
<meta property="og:url" content="http://tallgy.gitee.io/index.html">
<meta property="og:site_name" content="tallgy&#39;s blog">
<meta property="og:description" content="只是一个知识的搬运工">
<meta property="og:locale" content="zh_CN">
<meta property="article:author" content="tallgy">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://tallgy.gitee.io/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":true,"isPost":false,"lang":"zh-CN","comments":"","permalink":"","path":"index.html","title":""}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>tallgy's blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">tallgy's blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">tallgy</p>
  <div class="site-description" itemprop="description">只是一个知识的搬运工</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">107</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">32</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">74</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner index posts-expand">

    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2023/06/28/%E6%B5%8F%E8%A7%88%E5%99%A8/%E4%B8%89%E6%A0%8F%E5%B8%83%E5%B1%80/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2023/06/28/%E6%B5%8F%E8%A7%88%E5%99%A8/%E4%B8%89%E6%A0%8F%E5%B8%83%E5%B1%80/" class="post-title-link" itemprop="url">三栏布局</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2023-06-28 15:49:17 / 修改时间：18:09:04" itemprop="dateCreated datePublished" datetime="2023-06-28T15:49:17+08:00">2023-06-28</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="三栏布局"><a href="#三栏布局" class="headerlink" title="三栏布局"></a>三栏布局</h1><p>  三栏布局的样式是什么。<br><img src="/.io//image.png" alt="Alt text"></p>
<h2 id="使用-float-布局"><a href="#使用-float-布局" class="headerlink" title="使用 float 布局"></a>使用 float 布局</h2><p>让左侧和右侧使用 float 进行浮动，然后中间给一个 margin 双边的长度，进行布局。<br>需要让 mid DOM 放在后面。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html lang=&quot;en&quot;&gt;</span><br><span class="line">&lt;head&gt;</span><br><span class="line">  &lt;meta charset=&quot;UTF-8&quot;&gt;</span><br><span class="line">  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;</span><br><span class="line">  &lt;title&gt;Document&lt;/title&gt;</span><br><span class="line">&lt;/head&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">  .container &#123;</span><br><span class="line">  &#125;</span><br><span class="line">  .left &#123;</span><br><span class="line">    float: left;</span><br><span class="line">    width: 200px;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: aqua;</span><br><span class="line">  &#125;</span><br><span class="line">  .right &#123;</span><br><span class="line">    float: right;</span><br><span class="line">    width: 200px;</span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: aquamarine;</span><br><span class="line">  &#125;</span><br><span class="line">  .mid &#123;</span><br><span class="line">    margin-left: 220px;</span><br><span class="line">    margin-right: 220px;</span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: cadetblue;</span><br><span class="line">  &#125;</span><br><span class="line">&lt;/style&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">  &lt;div class=&quot;container&quot;&gt;</span><br><span class="line">    &lt;div class=&quot;left&quot;&gt;left&lt;/div&gt;</span><br><span class="line">    &lt;div class=&quot;right&quot;&gt;right&lt;/div&gt;</span><br><span class="line">    &lt;div class=&quot;mid&quot;&gt;mid&lt;/div&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line">&lt;/html&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="BFC-布局"><a href="#BFC-布局" class="headerlink" title="BFC 布局"></a>BFC 布局</h2><p>  通过使用 float 触发 BFC，然后让渲染层被隔开，同时<br>  float 脱离文档流，应该是会产生一个新的渲染层。</p>
<p>  什么是bfc（<a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_44165167/article/details/115617978%EF%BC%89">https://blog.csdn.net/weixin_44165167/article/details/115617978）</a></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">  .container &#123;</span><br><span class="line">  &#125;</span><br><span class="line">  .left &#123;</span><br><span class="line">    float: left;</span><br><span class="line">    width: 200px;</span><br><span class="line">    margin-right: 20px;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: aqua;</span><br><span class="line">  &#125;</span><br><span class="line">  .right &#123;</span><br><span class="line">    float: right;</span><br><span class="line">    width: 200px;</span><br><span class="line">    margin-left: 20px;</span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: aquamarine;</span><br><span class="line">  &#125;</span><br><span class="line">  .mid &#123;</span><br><span class="line">    overflow: hidden;</span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: cadetblue;</span><br><span class="line">  &#125;</span><br><span class="line">&lt;/style&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">  &lt;div class=&quot;container&quot;&gt;</span><br><span class="line">    &lt;div class=&quot;left&quot;&gt;left&lt;/div&gt;</span><br><span class="line">    &lt;div class=&quot;right&quot;&gt;right&lt;/div&gt;</span><br><span class="line">    &lt;div class=&quot;mid&quot;&gt;mid&lt;/div&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="flex-布局"><a href="#flex-布局" class="headerlink" title="flex 布局"></a>flex 布局</h2><p>  让父容器，设置为 display flex。然后子容器，设置为 flex-grow 为 1，让其自动填充。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">  .container &#123;</span><br><span class="line">    display: flex;</span><br><span class="line">  &#125;</span><br><span class="line">  .left &#123;</span><br><span class="line">    width: 200px;</span><br><span class="line">    margin-right: 20px;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: aqua;</span><br><span class="line">  &#125;</span><br><span class="line">  .right &#123;</span><br><span class="line">    width: 200px;</span><br><span class="line">    margin-left: 20px;</span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: aquamarine;</span><br><span class="line">  &#125;</span><br><span class="line">  .mid &#123;</span><br><span class="line">    flex-grow: 1;</span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: cadetblue;</span><br><span class="line">  &#125;</span><br><span class="line">&lt;/style&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">  &lt;div class=&quot;container&quot;&gt;</span><br><span class="line">    &lt;div class=&quot;left&quot;&gt;left&lt;/div&gt;</span><br><span class="line">    &lt;div class=&quot;mid&quot;&gt;mid&lt;/div&gt;</span><br><span class="line">    &lt;div class=&quot;right&quot;&gt;right&lt;/div&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="position-定位。"><a href="#position-定位。" class="headerlink" title="position 定位。"></a>position 定位。</h2><p>  让两边使用 position 绝对定位定位在两边，然后中间元素使用 margin 自动补充。<br>  父元素需要使用 position relative。因为解决子元素的 绝对定位的位置问题。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">  .container &#123;</span><br><span class="line">    position: relative;</span><br><span class="line">  &#125;</span><br><span class="line">  .left &#123;</span><br><span class="line">    position: absolute;</span><br><span class="line">    top: 0;</span><br><span class="line">    left: 0;</span><br><span class="line">    width: 200px;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: aqua;</span><br><span class="line">  &#125;</span><br><span class="line">  .right &#123;</span><br><span class="line">    position: absolute;</span><br><span class="line">    top: 0;</span><br><span class="line">    right: 0;</span><br><span class="line">    width: 200px;</span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: aquamarine;</span><br><span class="line">  &#125;</span><br><span class="line">  .mid &#123;</span><br><span class="line">    margin: 0 220px;</span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: cadetblue;</span><br><span class="line">  &#125;</span><br><span class="line">&lt;/style&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">  &lt;div class=&quot;container&quot;&gt;</span><br><span class="line">    &lt;div class=&quot;left&quot;&gt;left&lt;/div&gt;</span><br><span class="line">    &lt;div class=&quot;mid&quot;&gt;mid&lt;/div&gt;</span><br><span class="line">    &lt;div class=&quot;right&quot;&gt;right&lt;/div&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h2 id="table-布局"><a href="#table-布局" class="headerlink" title="table 布局"></a>table 布局</h2><p>  父元素使用 table 布局，然后子元素使用 table-cell，设置两侧子元素的宽度，父元素设置一个宽度，此时中间元素会自动进行填充。<br>  问题：table 布局是较为影响性能的。其次，无法设置间距。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">  .container &#123;</span><br><span class="line">    display: table;</span><br><span class="line">    width: 100%;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  .left, .right, .mid &#123;</span><br><span class="line">    display: table-cell;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  .left &#123;</span><br><span class="line">    width: 200px;</span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: aqua;</span><br><span class="line">  &#125;</span><br><span class="line">  .right &#123;</span><br><span class="line">    width: 200px;</span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: aquamarine;</span><br><span class="line">  &#125;</span><br><span class="line">  .mid &#123;</span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: cadetblue;</span><br><span class="line">  &#125;</span><br><span class="line">&lt;/style&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">  &lt;div class=&quot;container&quot;&gt;</span><br><span class="line">    &lt;div class=&quot;left&quot;&gt;left&lt;/div&gt;</span><br><span class="line">    &lt;div class=&quot;mid&quot;&gt;mid&lt;/div&gt;</span><br><span class="line">    &lt;div class=&quot;right&quot;&gt;right&lt;/div&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="圣杯布局"><a href="#圣杯布局" class="headerlink" title="圣杯布局"></a>圣杯布局</h2><p>  圣杯布局的核心是浮动、负边距、相对定位。<br>  将 left、right、mid 进行 float 布局，<br>  然后 将 left 的左边距设置为 -100%、 right 的 左边距设置为 -200px<br>  然后将父容器的左右边距为 200px，然后使用相对定位，让 left 左移200，right 右移200.<br>  这个需要让 mid DOM 在最前面。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">  .container &#123;</span><br><span class="line">    margin: 0 200px;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  .left &#123;</span><br><span class="line">    position: relative;</span><br><span class="line">    left: -200px;</span><br><span class="line">    float: left;</span><br><span class="line">    margin-left: -100%;</span><br><span class="line">    width: 200px;</span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: aqua;</span><br><span class="line">  &#125;</span><br><span class="line">  .right &#123;</span><br><span class="line">    position: relative;</span><br><span class="line">    right: -200px;</span><br><span class="line">    float: left;</span><br><span class="line">    width: 200px;</span><br><span class="line">    margin-left: -200px;</span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: aquamarine;</span><br><span class="line">  &#125;</span><br><span class="line">  .mid &#123;</span><br><span class="line">    float: left;</span><br><span class="line">    width: 100%;</span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: cadetblue;</span><br><span class="line">  &#125;</span><br><span class="line">&lt;/style&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">  &lt;div class=&quot;container&quot;&gt;</span><br><span class="line">    &lt;div class=&quot;mid&quot;&gt;mid&lt;/div&gt;</span><br><span class="line">    &lt;div class=&quot;left&quot;&gt;left&lt;/div&gt;</span><br><span class="line">    &lt;div class=&quot;right&quot;&gt;right&lt;/div&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<p>  如果 DOM 的元素位置 为 left mid right。<br>  那么 left 只需要 float 之后，设置相对定位并left -200.<br>  mid，float、ml-200<br>  right ：float、ml-200、相对定位 right -200<br>  父元素还是一样。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">  .container &#123;</span><br><span class="line">    margin: 0 200px;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  .left &#123;</span><br><span class="line">    position: relative;</span><br><span class="line">    left: -200px;</span><br><span class="line">    float: left;</span><br><span class="line">    width: 200px;</span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: aqua;</span><br><span class="line">  &#125;</span><br><span class="line">  .right &#123;</span><br><span class="line">    position: relative;</span><br><span class="line">    right: -200px;</span><br><span class="line">    margin-left: -200px;</span><br><span class="line">    float: left;</span><br><span class="line">    width: 200px;</span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: aquamarine;</span><br><span class="line">  &#125;</span><br><span class="line">  .mid &#123;</span><br><span class="line">    margin-left: -200px;</span><br><span class="line">    float: left;</span><br><span class="line">    width: 100%;</span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: cadetblue;</span><br><span class="line">  &#125;</span><br><span class="line">&lt;/style&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">  &lt;div class=&quot;container&quot;&gt;</span><br><span class="line">    &lt;div class=&quot;left&quot;&gt;left&lt;/div&gt;</span><br><span class="line">    &lt;div class=&quot;mid&quot;&gt;mid&lt;/div&gt;</span><br><span class="line">    &lt;div class=&quot;right&quot;&gt;right&lt;/div&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h2 id="双飞翼布局"><a href="#双飞翼布局" class="headerlink" title="双飞翼布局"></a>双飞翼布局</h2><p>  大致和圣杯布局一样，但是不对 left 和 right 使用 相对定位，通过对 mid 元素内部在创建一个元素，内部元素设置一个 margin 的左右边距进行处理。<br>  所以父容器也不需要给margin了。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">&lt;style&gt;</span><br><span class="line">  .container &#123;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  .left &#123;</span><br><span class="line">    margin-left: -100%;</span><br><span class="line">    float: left;</span><br><span class="line">    width: 200px;</span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: aqua;</span><br><span class="line">  &#125;</span><br><span class="line">  .right &#123;</span><br><span class="line">    margin-left: -200px;</span><br><span class="line">    float: left;</span><br><span class="line">    width: 200px;</span><br><span class="line"></span><br><span class="line">    height: 200px;</span><br><span class="line">    background-color: aquamarine;</span><br><span class="line">  &#125;</span><br><span class="line">  .mid &#123;</span><br><span class="line">    float: left;</span><br><span class="line">    width: 100%;</span><br><span class="line">    height: 200px;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  .midContent &#123;</span><br><span class="line">      height: 200px;</span><br><span class="line">      margin-left: 220px; </span><br><span class="line">      margin-right: 220px;</span><br><span class="line">      background-color: cadetblue;</span><br><span class="line">    &#125;</span><br><span class="line">&lt;/style&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&lt;body&gt;</span><br><span class="line">  &lt;div class=&quot;container&quot;&gt;</span><br><span class="line">    &lt;div class=&quot;mid&quot;&gt;</span><br><span class="line">      &lt;div class=&quot;midContent&quot;&gt;</span><br><span class="line">        mid</span><br><span class="line">      &lt;/div&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line">    &lt;div class=&quot;left&quot;&gt;left&lt;/div&gt;</span><br><span class="line">    &lt;div class=&quot;right&quot;&gt;right&lt;/div&gt;</span><br><span class="line">  &lt;/div&gt;</span><br><span class="line">&lt;/body&gt;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2023/06/27/%E6%B5%8F%E8%A7%88%E5%99%A8/html%E6%A0%87%E7%AD%BE%E7%9A%84async%E5%92%8Cdefer%E5%B1%9E%E6%80%A7/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2023/06/27/%E6%B5%8F%E8%A7%88%E5%99%A8/html%E6%A0%87%E7%AD%BE%E7%9A%84async%E5%92%8Cdefer%E5%B1%9E%E6%80%A7/" class="post-title-link" itemprop="url">html标签的async和defer属性</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2023-06-27 16:11:12 / 修改时间：19:33:58" itemprop="dateCreated datePublished" datetime="2023-06-27T16:11:12+08:00">2023-06-27</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/HTML/" itemprop="url" rel="index"><span itemprop="name">HTML</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/HTML/%E9%9A%8F%E7%AC%94/" itemprop="url" rel="index"><span itemprop="name">随笔</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <p>  首先 HTML 的解析流程是从上往下的。<br>  同时在遇到 script 标签的时候，会产生阻塞，因为 script 可以对 dom 进行操作，所以需要先将 script 里面的代码进行执行，避免后续重新构建 DOM 结构。<br>  当然 script 标签 也会让 css 标签被阻塞，原因同上一样。。</p>
<p>所以在以前的时候，script 标签往往都写在最后面，这样会最小化的影响 DOM 的生成，但是本质来说，放在最后面，存在 script 标签，其实还是会让 DOM 的生成结束被影响。并没有在本质上提升了速度。</p>
<p>所以就使用到了 async 和 defer 这两个的属性就是，让浏览器知道，这个 script 标签里面的内容不会影响到 DOM 的生成，可以被执行。（为什么会这样说，因为使用了这两个属性之后，js的执行就不是绝对的位置了，所以需要保证 js 不会影响 DOM 的生成。</p>
<p>async 表示，尽快执行。  （<a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/script#async%EF%BC%89">https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/script#async）</a><br>defer 表示，下载完之后，在浏览器DOM解析完成之后，在出发 DOMContentLoaded 事件之前被执行。 （<a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/script#defer%EF%BC%89">https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/script#defer）</a></p>
<p><img src="/.io//image.png" alt="Alt text"></p>
<p><img src="/.io//image1.png" alt="Alt text"></p>
<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/weixin_42655717/article/details/120329272">https://blog.csdn.net/weixin_42655717/article/details/120329272</a></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2023/06/27/JavaScript/Promise/Promise%E6%BA%90%E7%A0%81/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2023/06/27/JavaScript/Promise/Promise%E6%BA%90%E7%A0%81/" class="post-title-link" itemprop="url">未命名</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-06-27 16:00:29" itemprop="dateCreated datePublished" datetime="2023-06-27T16:00:29+08:00">2023-06-27</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2023/05/29/%E6%B5%8F%E8%A7%88%E5%99%A8/%E6%B5%8F%E8%A7%88%E5%99%A8%E6%B8%B2%E6%9F%93/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2023/05/29/%E6%B5%8F%E8%A7%88%E5%99%A8/%E6%B5%8F%E8%A7%88%E5%99%A8%E6%B8%B2%E6%9F%93/" class="post-title-link" itemprop="url">浏览器渲染</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-05-29 18:14:53" itemprop="dateCreated datePublished" datetime="2023-05-29T18:14:53+08:00">2023-05-29</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="浏览器渲染流程"><a href="#浏览器渲染流程" class="headerlink" title="浏览器渲染流程"></a>浏览器渲染流程</h1><h2 id="DOM-树解析"><a href="#DOM-树解析" class="headerlink" title="DOM 树解析"></a>DOM 树解析</h2><p>​    可以看看 vue html parser 的代码</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://github.com/vuejs/vue/blob/dev/src/compiler/parser/html-parser.js</span><br></pre></td></tr></table></figure>

<p>​    Dom 树主要是一个 堆栈处理结构。对标签进行堆栈存储，同时会创建对应的 dom 节点。 DOM 树的渲染会被js代码的执行所影响，这是因为js代码是可以操作 DOM 结构的，所以在为了保证不会因为js操作DOM结构所影响的问题，所以就会先停止DOM树的渲染。</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2023/05/26/CSS/CSS-position%E5%B1%9E%E6%80%A7/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2023/05/26/CSS/CSS-position%E5%B1%9E%E6%80%A7/" class="post-title-link" itemprop="url">CSS-position属性</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-05-26 15:37:16" itemprop="dateCreated datePublished" datetime="2023-05-26T15:37:16+08:00">2023-05-26</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="position的属性"><a href="#position的属性" class="headerlink" title="position的属性"></a>position的属性</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">static 正常</span><br><span class="line">relative 相对定位</span><br><span class="line">absolute 绝对定位</span><br><span class="line">fixed 固定定位</span><br><span class="line">sticky 粘性定位</span><br></pre></td></tr></table></figure>

<h2 id="static"><a href="#static" class="headerlink" title="static"></a>static</h2><pre><code>该关键字指定元素使用正常的布局行为，即元素在文档常规流中当前的布局位置。此时 top, right, bottom, left 和 z-index 属性无效。
</code></pre>
<h2 id="relative"><a href="#relative" class="headerlink" title="relative"></a>relative</h2><pre><code>该关键字下，元素先放置在未添加定位时的位置，再在不改变页面布局的前提下调整元素位置（因此会在此元素未添加定位时所在位置留下空白）。position:relative 对 table-*-group, table-row, table-column, table-cell, table-caption 元素无效。
</code></pre>
<h2 id="absolute"><a href="#absolute" class="headerlink" title="absolute"></a>absolute</h2><pre><code>元素会被移出正常文档流，并不为元素预留空间，通过指定元素相对于最近的非 static 定位祖先元素的偏移，来确定元素位置。绝对定位的元素可以设置外边距（margins），且不会与其他边距合并。
</code></pre>
<h2 id="fixed"><a href="#fixed" class="headerlink" title="fixed"></a>fixed</h2><pre><code>元素会被移出正常文档流，并不为元素预留空间，而是通过指定元素相对于屏幕视口（viewport）的位置来指定元素位置。元素的位置在屏幕滚动时不会改变。打印时，元素会出现在的每页的固定位置。fixed 属性会创建新的层叠上下文。当元素祖先的 transform、perspective、filter 或 backdrop-filter 属性非 none 时，容器由视口改为该祖先。
</code></pre>
<h2 id="sticky"><a href="#sticky" class="headerlink" title="sticky"></a>sticky</h2><p>元素根据正常文档流进行定位，然后相对它的最近滚动祖先（nearest scrolling ancestor）和 containing block（最近块级祖先 nearest block-level ancestor），包括 table-related 元素，基于 top、right、bottom 和 left 的值进行偏移。偏移值不会影响任何其他元素的位置。 该值总是创建一个新的层叠上下文（stacking context）。注意，一个 sticky 元素会“固定”在离它最近的一个拥有“滚动机制”的祖先上（当该祖先的 overflow 是 hidden、scroll、auto 或 overlay 时），即便这个祖先不是最近的真实可滚动祖先。这有效地抑制了任何“sticky”行为（详情见 Github issue on W3C CSSWG）。</p>
<h1 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h1><ul>
<li>脱离文档流的好处一定时间上可以提升浏览器的回流重绘</li>
</ul>
<h1 id="来源"><a href="#来源" class="headerlink" title="来源"></a>来源</h1><p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/position">https://developer.mozilla.org/zh-CN/docs/Web/CSS/position</a></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2023/05/26/JavaScript/JavaScript%E2%80%94instanceof%E4%BD%BF%E7%94%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2023/05/26/JavaScript/JavaScript%E2%80%94instanceof%E4%BD%BF%E7%94%A8/" class="post-title-link" itemprop="url">JavaScript—instanceof使用</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-05-26 14:57:36" itemprop="dateCreated datePublished" datetime="2023-05-26T14:57:36+08:00">2023-05-26</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="instancof"><a href="#instancof" class="headerlink" title="instancof"></a>instancof</h1><h2 id="作用"><a href="#作用" class="headerlink" title="作用"></a>作用</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">用于检测某个构造函数是否存在于某个实列的原型链中</span><br><span class="line">[source] instancof [target]</span><br><span class="line">代表了 target 的 原型 是否存在于 source 的原型链中。</span><br><span class="line"></span><br><span class="line">比如</span><br><span class="line"></span><br><span class="line">const a = new Array();</span><br><span class="line"></span><br><span class="line">console.log(a instanceof Array);  // true, 因为 Array.prototype 存在于 a 的原型链中</span><br><span class="line">console.log(a instanceof Date); // false, 因为 a 的原型链中没有 Date.prototype </span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="简单实现"><a href="#简单实现" class="headerlink" title="简单实现"></a>简单实现</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">const myInstanceof = (left, right) =&gt; &#123;</span><br><span class="line">    // 取出右侧的原型</span><br><span class="line">    const rightProto = right.prototype;</span><br><span class="line">    // 先取出左侧实例的原型链上的第一个原型。</span><br><span class="line">    let leftProto = left.__proto__;</span><br><span class="line"></span><br><span class="line">    // 判断是否存在，没有说明了检测完了也没有存在，直接return false；</span><br><span class="line">    while(leftProto) &#123;</span><br><span class="line">        // 相等，说明了 构造函数是存在于这个实例的原型链上的。</span><br><span class="line">        if (leftProto === rightProto) &#123;</span><br><span class="line">            return true;</span><br><span class="line">        &#125;</span><br><span class="line">        // 迭代赋值</span><br><span class="line">        leftProto = leftProto.__proto__;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    return false;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2023/05/25/React/setState%E8%A7%A3%E6%9E%90/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2023/05/25/React/setState%E8%A7%A3%E6%9E%90/" class="post-title-link" itemprop="url">setState解析</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-05-25 17:37:50" itemprop="dateCreated datePublished" datetime="2023-05-25T17:37:50+08:00">2023-05-25</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="是同步还是异步"><a href="#是同步还是异步" class="headerlink" title="是同步还是异步"></a>是同步还是异步</h2><ul>
<li>setState并不是单纯异步或者是同步的，它的表现会因为调用场景的不同而不同<ul>
<li>在react钩子函数中也就是我们说的生命周期函数里是setState异步的，还有合成事件也就是我们定义的函数，setState也是异步的。</li>
<li>而在setTimeout、setInterval等函数中，包括在DOM原生事件中，setState都是同步的。</li>
</ul>
</li>
</ul>
<p>原因</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">导致这种差异的原因，是因为react的事务机制和更新机制的工作方式决定的。事务机制，在源码中这个变量为 isBatchingUpdates ，在执行react钩子函数和合成事件之前，这个变量都会被react修改成true,当这个变量为true时，setState就不会生效，当钩子函数或者合成事件执行完毕之后，这个变量会被设置为false,此时setState才会生效，isBatchingUpdates就好像一把锁，在isBatchingUpdates的约束下setState只能是异步的。但是当遇到setTimeout时，事情就会有点不同，isBatchingUpdates的约束对setTimeout内部的执行逻辑完全没有约束能力,这是因为setTimeout就是异步的，当异步函数开始执行的时候，同步任务早就结束了，isBatchingUpdates早就被设置为了false。批量更新，每来一个setState，就会把他塞进一个队列里面，最后再合并相同任务，最后只针对需要更新的state进行操作</span><br><span class="line"></span><br></pre></td></tr></table></figure>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2022/04/04/Vue/vue-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8C%87%E4%BB%A4-loading%E7%9A%84%E4%BD%BF%E7%94%A8/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2022/04/04/Vue/vue-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%8C%87%E4%BB%A4-loading%E7%9A%84%E4%BD%BF%E7%94%A8/" class="post-title-link" itemprop="url">vue-自定义指令-loading的使用</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-04-04 01:42:56" itemprop="dateCreated datePublished" datetime="2022-04-04T01:42:56+08:00">2022-04-04</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Vue/" itemprop="url" rel="index"><span itemprop="name">Vue</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="vue-自定义指令-loading的使用"><a href="#vue-自定义指令-loading的使用" class="headerlink" title="vue-自定义指令-loading的使用"></a>vue-自定义指令-loading的使用</h1><p>​        首先，本片文章的核心其实就是 element-ui 的 v-loading 的一个源码查看 以及实现，因为组件需要使用 loading 加载。</p>
<p>​        这里就简单的说一些核心的部分内容</p>
<h2 id="自定义指令"><a href="#自定义指令" class="headerlink" title="自定义指令"></a>自定义指令</h2><p>​        首先就是，自定义指令。vue支持自定义指令，同时我们可以看到 element-ui 实际上使用的就是自定义指令。Vue.directive</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://cn.vuejs.org/v2/guide/custom-directive.html</span><br></pre></td></tr></table></figure>

<p>​        这个是一个官网的链接，简单说明了自定义指令的部分。知道自定义指令的一个大概流程。</p>
<p>​    简单来说，我们就是要简易实现一个 v-loading。然后我们查看钩子函数。大概就是需要 bind 和 unbind 两个。钩子函数。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// 注册一个全局自定义指令 `v-loading`</span><br><span class="line">Vue.directive(&#x27;loading&#x27;, &#123;</span><br><span class="line">  // 当被绑定的元素插入到 DOM 中时……</span><br><span class="line">  bind() &#123;&#125;</span><br><span class="line">  unbind() &#123;&#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>



<p>​        其实到了这里就已经差不多都知道了，大概就是在 bind 的时候通过js进行添加dom，然后再 unbind 的时候进行移除。</p>
<p>​        我这里实现了比较核心的几个部分。</p>
<ul>
<li>先使用 extend 创建一个类，然后我们通过创建这个类的实例创建dom。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">const Mask = Vue.extend(Loading)</span><br><span class="line"></span><br><span class="line">Vue.directive(&#x27;loading&#x27;, &#123;</span><br><span class="line">    bind(el, binding, vnode) &#123;</span><br><span class="line"></span><br><span class="line">      const mask = new Mask(&#123;</span><br><span class="line">      	// 代表挂载在一个新的 div 节点上面</span><br><span class="line">        el: document.createElement(&#x27;div&#x27;)</span><br><span class="line">      &#125;)</span><br><span class="line">      // $el 就是 实例的根元素</span><br><span class="line">      el.mask = mask.$el;</span><br><span class="line">			// 然后我们将这个 添加在 el 上面。</span><br><span class="line">			// 这里的 el 就是在 使用了 v-loading 的根节点。</span><br><span class="line">      el.appendChild(el.mask)</span><br><span class="line">    &#125;,</span><br><span class="line">    unbind(el, binding, vnode) &#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">// 然后我们可以通过 这个use 方法进行创建。创建插件。</span><br><span class="line">Vue.use(directive);</span><br></pre></td></tr></table></figure>


      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2022/04/03/%E7%AE%97%E6%B3%95/%E7%AE%80%E5%8D%95/9-%E5%9B%9E%E6%96%87%E6%95%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2022/04/03/%E7%AE%97%E6%B3%95/%E7%AE%80%E5%8D%95/9-%E5%9B%9E%E6%96%87%E6%95%B0/" class="post-title-link" itemprop="url">9-回文数</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-04-03 15:47:42" itemprop="dateCreated datePublished" datetime="2022-04-03T15:47:42+08:00">2022-04-03</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E7%AE%97%E6%B3%95/" itemprop="url" rel="index"><span itemprop="name">算法</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E7%AE%97%E6%B3%95/%E7%AE%80%E5%8D%95/" itemprop="url" rel="index"><span itemprop="name">简单</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="9-回文数"><a href="#9-回文数" class="headerlink" title="9-回文数"></a>9-回文数</h1><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://leetcode-cn.com/problems/palindrome-number/</span><br></pre></td></tr></table></figure>



<h2 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h2><p>给你一个整数 x ，如果 x 是一个回文整数，返回 true ；否则，返回 false 。</p>
<p>回文数是指正序（从左向右）和倒序（从右向左）读都是一样的整数。</p>
<p>例如，121 是回文，而 123 不是。</p>
<p>示例 1：</p>
<p>输入：x = 121<br>输出：true<br>示例 2：</p>
<p>输入：x = -121<br>输出：false<br>解释：从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。<br>示例 3：</p>
<p>输入：x = 10<br>输出：false<br>解释：从右向左读, 为 01 。因此它不是一个回文数。</p>
<h2 id="思路"><a href="#思路" class="headerlink" title="思路"></a>思路</h2><ul>
<li>首先，负数直接不是回文数。</li>
<li>其次，回文数，反过来的数是和原数相等。</li>
</ul>
<h3 id="转字符串，反转，在转数字"><a href="#转字符串，反转，在转数字" class="headerlink" title="转字符串，反转，在转数字"></a>转字符串，反转，在转数字</h3><p>​        知道了特点之后，我们可以先将数转字符串，反转为数字，然后和原数比较是否相等。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">仅仅只需要短短几行代码就可以</span><br><span class="line">var isPalindrome = function (x) &#123;</span><br><span class="line">	// 首先，小于零的，直接false，由题可知的。</span><br><span class="line">	// 这样可以减少后面的计算。</span><br><span class="line">  if (x &lt; 0) return false</span><br><span class="line"></span><br><span class="line">	// 然后，我们使用隐式转换为字符串</span><br><span class="line">	// 利用字符串的 split 进行分割为数组，</span><br><span class="line">	// 然后 reverse 反转数组</span><br><span class="line">	// 最后在 join 合并为字符串。</span><br><span class="line">  const reverse = (x+&#x27;&#x27;).split(&#x27;&#x27;).reverse().join(&#x27;&#x27;)</span><br><span class="line">  </span><br><span class="line">  // 最后使用 弱比较，== 内部会进行隐式转换，不需要我们在转换为数字。</span><br><span class="line">  return x == reverse;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="化为字符串，然后for-循环依次进行比较"><a href="#化为字符串，然后for-循环依次进行比较" class="headerlink" title="化为字符串，然后for 循环依次进行比较"></a>化为字符串，然后for 循环依次进行比较</h3><p>​        这个我就简单的写核心部分就行了。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// 就是简单的 循环进行比较。</span><br><span class="line">for (let i = 0; i &lt; str.length / 2; i++) &#123;</span><br><span class="line">  if (str[i] !== str[str.length - i - 1]) &#123;</span><br><span class="line">    return false</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="官方题解，反转一半的回文数"><a href="#官方题解，反转一半的回文数" class="headerlink" title="官方题解，反转一半的回文数"></a>官方题解，反转一半的回文数</h3><p>​        思路也不复杂，就是如果是回文数，那么反转一半，也应该是相等的。所以我们可以反转一半来进行判断。</p>
<p>​        我们最主要的问题就是，如果回文到了一半。这里直接说答案，就是如果我们回文数已经比原数大了，那么，说明回文到一半了。</p>
<p>​        这里我就不展示代码了。这个可以查看顶部原题链接查看官网的分析。</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://tallgy.gitee.io/2022/04/01/JavaScript/Promise/JavaScript-Promise-all%E6%BA%90%E7%A0%81/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tallgy">
      <meta itemprop="description" content="只是一个知识的搬运工">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tallgy's blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2022/04/01/JavaScript/Promise/JavaScript-Promise-all%E6%BA%90%E7%A0%81/" class="post-title-link" itemprop="url">JavaScript-Promise.all源码</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2022-04-01 14:28:27" itemprop="dateCreated datePublished" datetime="2022-04-01T14:28:27+08:00">2022-04-01</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2023-06-25 16:57:29" itemprop="dateModified" datetime="2023-06-25T16:57:29+08:00">2023-06-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/JavaScript/" itemprop="url" rel="index"><span itemprop="name">JavaScript</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/JavaScript/Promise/" itemprop="url" rel="index"><span itemprop="name">Promise</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="JavaScript-Promise-all源码"><a href="#JavaScript-Promise-all源码" class="headerlink" title="JavaScript-Promise.all源码"></a>JavaScript-Promise.all源码</h1><p>​        Promise，可以说是前端 js 面试里面的高频考点了。上次面试就让我手撕了一个Promise.all 方法的源码</p>
<p>​        这里，我先给出promise all方法的要求</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise/all</span><br></pre></td></tr></table></figure>

<ul>
<li>首先，大概就是一个可迭代的参数传递，返回一个promise的实例。对于所有都all时，返回resolve，对于失败，返回rejected。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">Promise.all = function(promises) &#123;</span><br><span class="line">  // 因为是可迭代的，同时参数可以是set，map等，所以我们可以先使用 ... 进行全部划分为数组</span><br><span class="line">  const requests = [...promises]</span><br><span class="line"></span><br><span class="line">  // 同时因为返回是 promise，所以</span><br><span class="line">  return new Promise((resolve, reject) =&gt; &#123;</span><br><span class="line">    </span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>其次，需要进行依次的请求，在 res push进返回的resolve数组，err，直接reject回去。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">Promise.all = function(promises) &#123;</span><br><span class="line">  // 因为是可迭代的，同时参数可以是set，map等，所以我们可以先使用 ... 进行全部划分为数组</span><br><span class="line">  const requests = [...promises]</span><br><span class="line">  const resArr = []</span><br><span class="line"></span><br><span class="line">  // 同时因为返回是 promise，所以</span><br><span class="line">  return new Promise((resolve, reject) =&gt; &#123;</span><br><span class="line">  	// 依次进行循环，操作</span><br><span class="line">    for (let i = 0; i &lt; requests.length; i++) &#123;</span><br><span class="line">      const request = requests[i]</span><br><span class="line">      // 判断为 then 还是 catch</span><br><span class="line">      request.then(res =&gt; &#123;</span><br><span class="line">      	// res，push进数组</span><br><span class="line">        resArr.push(res)</span><br><span class="line">      &#125;, err =&gt; &#123;</span><br><span class="line">      	// catch 直接 rejected</span><br><span class="line">        reject(err)</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>​        这里，我们有了一个初步的，然后就是需要判断如果push全部都结束了，那么就应该resolve数组了。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">count++;</span><br><span class="line">resArr.push(res)</span><br><span class="line">if (count === requests.length) resolve(resArr)</span><br></pre></td></tr></table></figure>

<p>​        这里也没有什么，就是进行push，然后判断是否push结束了。</p>
<p>​        写到这里，突然发现一个问题，那就是，不能使用 push 啊，因为异步的不确定性，需要使用下标进行赋值，不然返回的结果会是乱的。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">Promise.all = function(promises) &#123;</span><br><span class="line">  // 因为是可迭代的，同时参数可以是set，map等，所以我们可以先使用 ... 进行全部划分为数组</span><br><span class="line">  const requests = [...promises]</span><br><span class="line">  const resArr = []</span><br><span class="line">  let count = 0;</span><br><span class="line"></span><br><span class="line">  // 同时因为返回是 promise，所以</span><br><span class="line">  return new Promise((resolve, reject) =&gt; &#123;</span><br><span class="line">    for (let i = 0; i &lt; requests.length; i++) &#123;</span><br><span class="line">      const request = requests[i]</span><br><span class="line">      request.then(res =&gt; &#123;</span><br><span class="line">        count++;</span><br><span class="line">        // resArr.push(res)</span><br><span class="line">        resArr[i] = res</span><br><span class="line">        if (count === requests.length) resolve(resArr)</span><br><span class="line">      &#125;, err =&gt; &#123;</span><br><span class="line">        reject(err)</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>​        到了这里，感觉快差不多了，但是我们还需要对传递的参数进行一个判断，对于不是Promise的，我们需要直接resolve成功。</p>
<p>​        创建一个 promise判断的函数。这里有两个思路。</p>
<ul>
<li>判断 存在req，同时 then 为function</li>
<li>判断 req为对象，同时 then为function。</li>
</ul>
<p>当然，最后我选择了第一个，因为判断req为对象，需要使用Object.prototype.toString 方法，因为 typeof 对 null 为 object，这里就需要进行额外思考，不然会 req.then 报错。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">function isPromise(req) &#123;</span><br><span class="line">  // req 是一个对象，同时then是一个方法。</span><br><span class="line">  if (req &amp;&amp; typeof req.then === &#x27;function&#x27;) &#123;</span><br><span class="line">    return true</span><br><span class="line">  &#125;</span><br><span class="line">  return false</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>​        创建一个添加进入数组的函数</p>
<p>​    这里，我们将resArr[i] 直接复制为 res。可以避免异步产生返回结果不同步出现的问题。</p>
<p>​    同时我们是通过返回一个 bool 来判断运行是否应该结束，当然。这个问题在于我们在resolve之后，可能是最后一个resolve，所以需要再resolve出去，</p>
<p>​    这里有两个方法</p>
<ul>
<li>第一个就是我现在的写法，通过返回bool，判断是否结束，然后在另一个地方进行resolve 处理</li>
<li>第二个就是利用了闭包的效果，我们将函数写在了 return new Promise(() =&gt; {}) 里面，这样就可以使用闭包，我们只需要在函数里面进行判断是否应该resolve，而不是在外面添加一个判断。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">// 进行添加到resArr数组里面，同时判断是否为最后一个</span><br><span class="line">function requestResolve(res, i) &#123;</span><br><span class="line">  // count++;</span><br><span class="line">  resArr[i] = res</span><br><span class="line">  return ++count === requests.length;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>​        其实，这里我也不好决定使用哪一种，因为我的代码的切割水平不到位，不知道这个 resolve，应该放在 new promise里面，还是说放在方法内部，主要是放在方法内部，给我一种逻辑被划开了的感觉。</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line">Promise.all = function(promises) &#123;</span><br><span class="line">  // 因为是可迭代的，同时参数可以是set，map等，所以我们可以先使用 ... 进行全部划分为数组</span><br><span class="line">  const requests = [...promises]</span><br><span class="line">  const resArr = []</span><br><span class="line">  let count = 0;</span><br><span class="line"></span><br><span class="line">  function isPromise(req) &#123;</span><br><span class="line">    // req 是一个对象，同时then是一个方法。</span><br><span class="line">    if (typeof req === &#x27;object&#x27;</span><br><span class="line">        &amp;&amp; typeof req.then === &#x27;function&#x27;) &#123;</span><br><span class="line">      return true</span><br><span class="line">    &#125;</span><br><span class="line">    return false</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  // 进行添加到resArr数组里面，同时判断是否为最后一个</span><br><span class="line">  function requestResolve(res, i) &#123;</span><br><span class="line">    // count++;</span><br><span class="line">    resArr[i] = res</span><br><span class="line">    return ++count === requests.length;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  // 同时因为返回是 promise，所以</span><br><span class="line">  return new Promise((resolve, reject) =&gt; &#123;</span><br><span class="line">    for (let i = 0; i &lt; requests.length; i++) &#123;</span><br><span class="line">      const request = requests[i]</span><br><span class="line">      if (isPromise(request)) &#123;</span><br><span class="line">        request.then(res =&gt; &#123;</span><br><span class="line">        	// 这里我是将其分割开来，感觉这样逻辑是分开的。</span><br><span class="line">          requestResolve(res, i) &amp;&amp; resolve(resArr)</span><br><span class="line">        &#125;, err =&gt; &#123;</span><br><span class="line">          reject(err)</span><br><span class="line">        &#125;)</span><br><span class="line">        // err 可以直接使用 reject</span><br><span class="line">        // req.then(res =&gt; &#123;&#125;, reject)。</span><br><span class="line">        // 高级，看了别人的代码就是不一样。</span><br><span class="line">      &#125; else &#123;</span><br><span class="line">        requestResolve(request, i) &amp;&amp; resolve(resArr)</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>






      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




  <nav class="pagination">
    <span class="page-number current">1</span><a class="page-number" href="/page/2/">2</a><span class="space">&hellip;</span><a class="page-number" href="/page/11/">11</a><a class="extend next" rel="next" href="/page/2/"><i class="fa fa-angle-right" aria-label="下一页"></i></a>
  </nav>

</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tallgy</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
